package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import pojo.Area;
import util.DBUtil;
import dao.AreaDao;

public class AreaDaoImpl implements AreaDao {
	
	private static final Logger LOGGER = Logger.getLogger(AreaDaoImpl.class);

	@Override
	public List<Area> findByKeyword(String keyword) {
		List<Area> areas = new ArrayList<Area>();
		
		Connection conn = null;
		String sql = "select * from areas where area like ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.createConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, keyword + "%");
			rs = ps.executeQuery();
			while (rs.next()) {
				Area a = new Area();
				a.setId(rs.getString("id"));
				a.setAreaid(rs.getString("areaid"));
				a.setArea(rs.getString("area"));
				a.setCityid(rs.getString("cityid"));
				areas.add(a);
			}
		} catch (SQLException e) {
			LOGGER.error("SQL error", e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		
		return areas;
	}

}
